package com.compomics.proteocloud.db.accessor;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Taxon extends TaxonTableAccessor {
	 /**
     * Calls the super class.
     */
    public Taxon(){
        super();
    }

    /**
     * Calls the super class.
     * @param params
     */
    public Taxon(HashMap params){
        super(params);
    }
    
    /**
     * This constructor reads the spectrum file from a resultset. The ResultSet should be positioned such that a single
     * row can be read directly (i.e., without calling the 'next()' method on the ResultSet).
     *
     * @param aRS ResultSet to read the data from.
     * @throws SQLException when reading the ResultSet failed.
     */
    public Taxon(ResultSet aRS) throws SQLException {
        super(aRS);
    }
    
    /**
     * This method will find a spectrum file from the current connection, based on the filename.
     *
     * @param species String with the species of the spectrum file to find.
     * @param aConn     Connection to read the spectrum File from.
     * @return Spectrumfile with the data.
     * @throws SQLException when the retrieval did not succeed.
     */
    public static Taxon findFromSpecies(String species, Connection aConn) throws SQLException {
    	Taxon temp = null;
        PreparedStatement ps = aConn.prepareStatement(getBasicSelect() + " where species = ?");
        ps.setString(1, species);
        ResultSet rs = ps.executeQuery();
        int counter = 0;
        while (rs.next()) {
            counter++;
            temp = new Taxon(rs);
        }
        rs.close();
        ps.close();

        return temp;
    }

    /**
     * This method allows the caller to obtain all rows for this
     * table from a persistent store.
     *
     * @param   aConn Connection to the persitent store.
     * @return   List<Taxon>   with all entries for this table.
     */
    public static List<Taxon> getAllTaxa(Connection aConn) throws SQLException {
        List<Taxon>  entities = new ArrayList<Taxon>();
        Statement stat = aConn.createStatement();
        ResultSet rs = stat.executeQuery(getBasicSelect());
        while(rs.next()) {
            entities.add(new Taxon(rs));
        }
        rs.close();
        stat.close();
        return entities;
    }
}
